let dateUtil = {}

/**
 *  日期格式化
 *  option 格式化参数 Y=>年（2018）,M=>月(11)，D=>日(09)，H=>小时（11）,m=>分钟，s=>秒，S=>毫秒(234)
 * */
dateUtil.format = function (dateStr, option) {
    option = option || 'Y-M-D'

    var date = new Date(dateStr)
    // 年
    option = option.replace(new RegExp('[Yy]', 'g'), date.getFullYear())

    // 月
    var month = date.getMonth() + 1
    month = month < 10 ? '0' + month : month
    option = option.replace(new RegExp('[M]', 'g'), month)

    // 日
    var day = date.getDate()
    day = day < 10 ? '0' + day : day
    option = option.replace(new RegExp('[Dd]', 'g'), day)

    // 时
    let hour = date.getHours()
    hour = hour < 10 ? ('0' + hour) : hour
    option = option.replace(new RegExp('[Hh]', 'g'), hour)

    // 分
    let minute = date.getMinutes()
    minute = minute < 10 ? ('0' + minute) : minute
    option = option.replace(new RegExp('[m]', 'g'), minute)

    // 秒
    let second = date.getSeconds()
    second = second < 10 ? ('0' + second) : second
    option = option.replace(new RegExp('[s]', 'g'), second)

    // 毫秒
    let millisecond = date.getMilliseconds()
    millisecond = millisecond < 100 ? (millisecond < 10 ? '00' + millisecond : '0' + millisecond) : millisecond
    option = option.replace(new RegExp('[S]', 'g'), millisecond)

    return option
}

// 获取指定日期
dateUtil.getSetDate = function (calcDays, option) {
    option = option || 'Y-M-D'
    calcDays = calcDays || 0

    var curr = new Date().getTime()
    var calcSeconds = calcDays * 86400000
    var resTime = curr + calcSeconds

    return dateUtil.format(new Date(resTime), option)
}

export default dateUtil
